package base.thread_JUC2.chapter03.demo07;

import java.util.List;
import java.util.concurrent.Exchanger;

public class Producer implements Runnable{
    private List<String> list;
    private Exchanger<List<String>> exchanger;

    public Producer(List<String> list, Exchanger<List<String>> exchanger) {
        this.list = list;
        this.exchanger = exchanger;
    }

    @Override
    public void run() {
        int cycle=1;

        for (int i = 0; i < 10; i++) {
            System.out.printf("Producer: Cycle %d\n",cycle);

            for (int j = 0; j < 5; j++) {
                String message = "Event "+ (i*10 + j);
                list.add(message);
            }

            try {
                exchanger.exchange(list);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.printf("Producer: %d\n",list.size());
            cycle++;
        }
    }
}
